package com.eff.android.fragment.options;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.eff.android.R;
import com.eff.android.api.enums.Subject;
import com.eff.android.api.model.Setting;
import com.eff.android.fragment.EBaseFragment;

public class EMainFragmentOptionsSubjects extends EBaseFragment{
	public static final String TAG = EMainFragmentOptionsSubjects.class.getSimpleName();
	
	RadioGroup group;
	Setting settings;
	
	public static EMainFragmentOptionsSubjects newInstance(Bundle b) {
		EMainFragmentOptionsSubjects f = new EMainFragmentOptionsSubjects();
		if (b != null){
		    f.setArguments(b);
		}
		return f;
	}

	public static Bundle getArgs() {
		Bundle b = new Bundle();
		b.putString(TAG, TAG);
		return b;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		settings = (Setting) this.getArguments().getSerializable(Setting.TAG);
		Log.d("Bien chua kieu du lieu type", settings.getType().getValue());
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.e_frm_main_options_subjects, container, false);
		setViewGroupRadioButton(v);
		    
		return v;
    }
	
	private void setViewGroupRadioButton(View v){
		group = (RadioGroup)v.findViewById(R.id.radioGroupSubject);
		
		String currentSubject = settings.getSubject().name().toLowerCase();
		Log.d("Current subject", currentSubject);
		
		for (int i=0; i<group.getChildCount(); i++){
			RadioButton radioBtn = (RadioButton) group.getChildAt(i);
			Log.d("Radio Button text", radioBtn.getText().toString().toLowerCase());
			
			if (radioBtn.getText().toString().toLowerCase().equals(currentSubject)){
				group.check(radioBtn.getId());
				break;
			}
		}
		
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				setSubjects(checkedId);
			}
		});
	}
	
	private void setSubjects (int checkedId){
		switch (checkedId){
		case R.id.radioPeople:
			Log.d("Ham setSubject radioPeople", "People");
			settings.setSubject(Subject.People);
			break;
		case R.id.radioNature:
			Log.d("Ham setSubject radioNature", "Nature");
			settings.setSubject(Subject.nature);
			break;
		case R.id.radioActivitive:
			Log.d("Ham setSubject radioActivitive", "Activity");
			settings.setSubject(Subject.Activitive);
			break;
		case R.id.radioCommunication:
			settings.setSubject(Subject.Communication);
			break;
		case R.id.radioCharacter:
			settings.setSubject(Subject.Character);
			break;
		case R.id.radioMixed:
			settings.setSubject(Subject.Mixed);
			break;
		}
		Log.d("Setting subject", settings.getSubject().name());
	}

}
